package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BFeeModelEnum;
import cn.huiyunche.base.service.enums.BFeeTypeEnum;
import cn.huiyunche.base.service.interfaces.BFeeModelService;
import cn.huiyunche.base.service.interfaces.BFeeTypeService;
import cn.huiyunche.base.service.interfaces.SBaseFeeService;
import cn.huiyunche.base.service.mappers.SBaseFeeMapper;
import cn.huiyunche.base.service.model.BFeeType;
import cn.huiyunche.base.service.model.SBaseFee;
import cn.huiyunche.base.service.model.SBaseFeeExample;
import cn.huiyunche.base.service.vo.SOrderFeeDetailVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/18 下午3:03
 */
@Service
public class SBaseFeeServiceImpl implements SBaseFeeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SBaseFeeServiceImpl.class);

    @Autowired
    private SBaseFeeMapper sBaseFeeMapper;

    @Autowired
    private BFeeTypeService bFeeTypeService;

    @Autowired
    private BFeeModelService bFeeModelService;

    @Override
    public SOrderFeeDetailVo queryBaseFee(String areaCode) {
        LOGGER.info("queryBaseFee param : {}", areaCode);
        if (StringUtils.isBlank(areaCode)) {
            LOGGER.error("queryBaseFee param areaCode must not be null");
            throw new IllegalArgumentException("区域编码不能为空");
        }

        //查询费用类型信息
        BFeeType feeType = bFeeTypeService.getByCode(bFeeModelService.getByCode(BFeeModelEnum.CITYWIDE_ORDER).getId(), Arrays.asList(BFeeTypeEnum.STARTING_PRICE.getValue())).get(0);

        SOrderFeeDetailVo vo = new SOrderFeeDetailVo();
        vo.setId(feeType.getId());
        vo.setFeeCode(feeType.getFeeCode());
        vo.setFeeName(feeType.getFeeName());

        SBaseFee baseFee = this.getByAreaCode(areaCode);

        vo.setFeeName(vo.getFeeName() + "(" + baseFee.getBaseKm() + "公里)");
        vo.setCost(baseFee.getBasePrice());
        vo.setActualAmount(vo.getCost());

        return vo;
    }

    @Override
    public SBaseFee getByAreaCode(String areaCode) {
        LOGGER.info("getByAreaCode param : {}", areaCode);
        if (StringUtils.isBlank(areaCode)) {
            LOGGER.error("getByAreaCode param areaCode must not be null");
            throw new IllegalArgumentException("区域编码不能为空");
        }

        SBaseFeeExample example = new SBaseFeeExample();
        example.createCriteria().andBAreaCodeEqualTo(areaCode);

        List<SBaseFee> list = sBaseFeeMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            LOGGER.error("queryBaseFee list must not be null");
            throw new IllegalArgumentException("查询起步价失败");
        }

        return list.get(0);
    }
}
